package com.example.graduatetest.model;

import android.text.TextUtils;
import android.util.Log;

import com.example.graduatetest.bean.Card;
import com.example.graduatetest.bean.Classes;
import com.example.graduatetest.bean.Score;
import com.example.graduatetest.callback.ClassList;
import com.example.graduatetest.callback.ScoreCallBack;
import com.example.graduatetest.callback.ScoreListCallBack;
import com.example.graduatetest.callback.StringCallBack;
import com.example.graduatetest.callback.TestNameList;
import com.example.graduatetest.utils.Constant;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class ScoreModel {
    private static String www = Constant.www;
    private static String insertOneScoreUrl = "/score/insertOneScore";
    private static String choiceFormat;
    private static String selectOneScoreUrl = "/score/selectOnePersonScore";
    private static String selectScoreListUrl = "/score/selectScoreList";
    private static String selectScoreMaxUrl = "/score/selectScoreMax";
    private static String selectTestNameUrl = "/score/selectTestName";
    //查询最大成绩
    public static void selectScoreMax(final String class_number,
                                    final String test_name,
                                    final StringCallBack callBack){
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient okHttpClient = new OkHttpClient();
                Request.Builder reqBuild = new Request.Builder();
                HttpUrl.Builder urlBuilder = HttpUrl.parse(www+selectScoreMaxUrl)
                        .newBuilder();
                urlBuilder.addQueryParameter("class_number", class_number);
                urlBuilder.addQueryParameter("test_name", test_name);
                reqBuild.url(urlBuilder.build());
                Request request = reqBuild.build();
                Call call = okHttpClient.newCall(request);
                try {
                    Response response = call.execute();
                    String jsonData = response.body().string();
                    callBack.onSuccess(jsonData);
                } catch (IOException e) {
                    callBack.onFailed();
                    e.printStackTrace();
                }
            }
        }).start();
    }
    //查询多个成绩
    public static void selectScores(final String class_number,
                                    final String test_name,
                                    final ScoreListCallBack callBack){
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient okHttpClient = new OkHttpClient();
                Request.Builder reqBuild = new Request.Builder();
                HttpUrl.Builder urlBuilder = HttpUrl.parse(www+selectScoreListUrl)
                        .newBuilder();
                urlBuilder.addQueryParameter("class_number", class_number);
                urlBuilder.addQueryParameter("test_name", test_name);
                reqBuild.url(urlBuilder.build());
                Request request = reqBuild.build();
                Call call = okHttpClient.newCall(request);
                try {
                    Response response = call.execute();
                    String jsonData = response.body().string();
                    parseJSONScoreList(jsonData,callBack);
                } catch (IOException e) {
                    callBack.onFailed();
                    e.printStackTrace();
                }
            }
        }).start();
    }
    //查询考试名称
    public static void selectTestName(final String class_number,
                                      final TestNameList callBack){

        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient okHttpClient = new OkHttpClient();
                Request.Builder reqBuild = new Request.Builder();
                HttpUrl.Builder urlBuilder = HttpUrl.parse(www+selectTestNameUrl)
                        .newBuilder();
                urlBuilder.addQueryParameter("class_number", class_number);
                reqBuild.url(urlBuilder.build());
                Request request = reqBuild.build();
                Call call = okHttpClient.newCall(request);
                try {
                    Response response = call.execute();
                    String jsonData = response.body().string();
                    parseJSONStringList(jsonData, callBack);
                } catch (IOException e) {
                    callBack.onFailed();
                    e.printStackTrace();
                }
            }
        }).start();
    }

    //查询单个成绩
    public static void selectOneScore(final String student_number,
                                      final String class_number,
                                      final String test_name,
                                      final ScoreCallBack callBack){
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient okHttpClient = new OkHttpClient();
                Request.Builder reqBuild = new Request.Builder();
                HttpUrl.Builder urlBuilder = HttpUrl.parse(www+selectOneScoreUrl)
                        .newBuilder();
                urlBuilder.addQueryParameter("student_number", student_number);
                urlBuilder.addQueryParameter("class_number", class_number);
                urlBuilder.addQueryParameter("test_name", test_name);
                reqBuild.url(urlBuilder.build());
                Request request = reqBuild.build();
                Call call = okHttpClient.newCall(request);
                try {
                    Response response = call.execute();
                    String jsonData = response.body().string();
                    parseJsonScore(jsonData,callBack);
                } catch (IOException e) {
                    callBack.onFailed();
                    e.printStackTrace();
                }
            }
        }).start();

    }
    //插入成绩
    public static void insertOneScore(final String class_number,
                                      final String student_number,
                                      final String test_name,
                                      final String score,
                                      final String class_name,
                                      final boolean isHundred,
                                      final StringCallBack callBack){
        if(isHundred){
           choiceFormat = "百分制";
        }else {
            choiceFormat = "一百五十分制";
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    OkHttpClient client = new OkHttpClient();//新建一个OKHttp的对象
                    RequestBody body = new FormBody.Builder()
                            .add("class_number", class_number)
                            .add("class_name", class_name)
                            .add("test_name", test_name)
                            .add("score", score)
                            .add("student_number", student_number)
                            .add("score_format",choiceFormat)
                            .build();

                    Request request = new Request.Builder()
                            .url(www+insertOneScoreUrl)
                            .post(body)
                            .build();//创建一个Request对象

                    Response response = client.newCall(request).execute();//发送请求获取返回数据
                    String responseData = response.body().string();//处理返回的数据
                    Log.e("haha", responseData);
                    callBack.onSuccess(responseData);

                }catch (Exception e){
                    callBack.onFailed();
                    e.printStackTrace();
                }
            }
        }).start();

    }

    private static void parseJsonScore(String jsonData,ScoreCallBack callBack){
        try{
            if(!TextUtils.isEmpty(jsonData)){
                JSONObject jsonObject = new JSONObject(jsonData);
                Gson gson = new Gson();//实例化gson
                //解析card对象
                Score score = gson.fromJson(jsonObject.toString(),Score.class);
                callBack.onSuccess(score);
            }else{
                callBack.onFailed();
            }
        }catch (Exception e){
            callBack.onFailed();
            e.printStackTrace();
        }
    }
    //解析json对象
    private static void parseJSONScoreList(String jsonData, ScoreListCallBack callBack){
        try{
            JSONArray jsonArray = new JSONArray(jsonData);
            Gson gson = new Gson();//实例化gson
            //解析Score List对象
            Log.e("haha", jsonArray.toString());
            List<Score> scoreList = gson.fromJson(jsonArray.toString(), new TypeToken<List<Score>>(){}.getType());
            callBack.onSuccess(scoreList);
        }catch (Exception e){
            callBack.onFailed();
            e.printStackTrace();
        }
    }

    //解析String对象
    private static void parseJSONStringList(String jsonData, TestNameList callBack){
        try{
            JSONArray jsonArray = new JSONArray(jsonData);
            Gson gson = new Gson();//实例化gson
            List<String> nameList = gson.fromJson(jsonArray.toString(), new TypeToken<List<String>>(){}.getType());
            callBack.onSuccess(nameList);
            Log.e("haha",nameList.toString());
        }catch (Exception e){
            callBack.onFailed();
            e.printStackTrace();
        }
    }
}
